﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using Model;
using Newtonsoft.Json.Linq;

namespace Controller.Helper
{
    public class Sale
    {
        public static string GetCustomerBySale(string salegrp)
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allCustomer = from cust in nawaDataContext.tbl_msa122_customer_sales
                                  join custdetail in nawaDataContext.tbl_msa120_customer_generals on cust.mcustno equals custdetail.mcustno
                                  where cust.msalegrp.ToLower().Equals(salegrp.ToLower())
                                  select new
                                  {
                                      cust.msalegrp,
                                      cust.mcustno,
                                      cust.msaleorg,
                                      cust.mdc,
                                      cust.mpricegrp,
                                      custdetail.mcustname1
                                  };

                JObject k = JObject.FromObject(new
                {
                    AllCustomer = allCustomer
                });
                return k.ToString();
            }
        }

        public static string GetCustomerBySale(string dc, string salegrp)
        {
            var doctype = HttpContext.Current.Request.Cookies.Get("apptype");
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allCustomer = from cust in nawaDataContext.tbl_msa122_customer_sales
                                  join custdetail in nawaDataContext.tbl_msa120_customer_generals on cust.mcustno equals custdetail.mcustno
                                  where cust.msalegrp.ToLower().Equals(salegrp.ToLower()) && cust.mdc.Equals(dc)
                                  select new
                                  {
                                      cust.msalegrp,
                                      cust.mcustno,
                                      cust.msaleorg,
                                      cust.mdc,
                                      cust.mpricegrp,
                                      custdetail.mcustname1
                                  };

                JObject k = JObject.FromObject(new
                {
                    AllCustomer = allCustomer
                });
                return k.ToString();
            }
        }
        public static string GetCustomerBySale(string dc, string salegrp, string docType)
        {
            if (string.IsNullOrEmpty(docType) || docType == "01")
            {
                return GetCustomerBySale(dc, salegrp);
            }
            else
            {
                using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
                {
                    var allCustomer = (from cust in nawaDataContext.tbl_msa122_customer_sales
                                       join custdetail in nawaDataContext.tbl_msa122_customer_sale_pricegroups on
                                           cust.mpricegrp
                                           equals custdetail.mpricegrp
                                       where cust.msalegrp.ToLower().Equals(salegrp.ToLower()) && cust.mdc.Equals(dc)
                                       select new
                                                  {
                                                      cust.msalegrp,
                                                      cust.msaleorg,
                                                      cust.mdc,
                                                      mcustno = cust.mpricegrp,
                                                      mcustname1 = custdetail.mpricegrp
                                                  }).Distinct();

                    JObject k = JObject.FromObject(new
                                                       {
                                                           AllCustomer = allCustomer
                                                       });
                    return k.ToString();
                }
            }
        }

        public static string GetAllDcCustomerBySale(string salegrp, string docType)
        {
            if (string.IsNullOrEmpty(docType) || docType == "01")
            {
                using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
                {
                    var allCustomer = from cust in nawaDataContext.tbl_msa122_customer_sales
                                      join custdetail in nawaDataContext.tbl_msa120_customer_generals on cust.mcustno equals custdetail.mcustno
                                      where cust.msalegrp.ToLower().Equals(salegrp.ToLower()) 
                                      select new
                                      {
                                          cust.msalegrp,
                                          cust.mcustno,
                                          cust.msaleorg,
                                          cust.mdc,
                                          cust.mpricegrp,
                                          custdetail.mcustname1
                                      };

                    JObject k = JObject.FromObject(new
                    {
                        AllCustomer = allCustomer
                    });
                    return k.ToString();
                }
            }
            else
            {
                using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
                {
                    var allCustomer = (from cust in nawaDataContext.tbl_msa122_customer_sales
                                       join custdetail in nawaDataContext.tbl_msa122_customer_sale_pricegroups on
                                           cust.mpricegrp
                                           equals custdetail.mpricegrp
                                       where cust.msalegrp.ToLower().Equals(salegrp.ToLower()) 
                                       select new
                                       {
                                           cust.msalegrp,
                                           cust.msaleorg,
                                           cust.mdc,
                                           mcustno = cust.mpricegrp,
                                           mcustname1 = custdetail.mpricegrp
                                       }).Distinct();

                    JObject k = JObject.FromObject(new
                    {
                        AllCustomer = allCustomer
                    });
                    return k.ToString();
                }
            }
        }

        //public static string GetAllSale()
        //{
        //    using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
        //    {
        //        var allSale = from sale in nawaDataContext.tbl_msa331_pipe_salegrps

        //                      select sale;

        //        JObject k = JObject.FromObject(new
        //        {
        //            AllSale = allSale
        //        });
        //        return k.ToString();
        //    }
        //}

        public static string GetAllSale()
        {
            using (DCSalePriceDataContext dataContext = new DCSalePriceDataContext())
            {
                string SaleRoleName = WebConfigurationManager.AppSettings["SaleRoleName"] ?? "Sale";
                var sales = from approvals in dataContext.tblUsers
                            where approvals.RoleName.Equals(SaleRoleName) && approvals.IsDisable != true
                            select approvals;

                JObject k = JObject.FromObject(new
                {
                    AllSale = sales
                });
                return k.ToString();
            }
        }
        public static List<tblUser> GetSaleCoBySale(string saleUser)
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allSaleco = from saleMapping in nawaDataContext.tblSaleCoSaleMappings
                                join user in nawaDataContext.tblUsers on saleMapping.SaleCoUserName equals user.UserName
                                where saleMapping.SaleUserName.Equals(saleUser) && user.IsDisable != true
                                select user;
                
                return allSaleco.ToList();
            }
        }

    }
}
